package com.cjy.discuss.service.impl;

import com.cjy.discuss.dao.CommentRepository;
import com.cjy.discuss.entity.Comment;
import com.cjy.discuss.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private CommentRepository commentRepository;
    /**
     * 查询所有
     *
     * @return
     */
    public List<Comment> findAll() {
        return mongoTemplate.findAll(Comment.class);
    }


    /**
     * 添加评论
     */
    public void save(Comment comment){
          comment.setParentid("1");
          comment.setReplynum(0);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-hh");
        comment.setCreatedatetime(simpleDateFormat.format(new Date()));
          comment.setLikenum(50);
          comment.setNickname("lhk");
        mongoTemplate.save(comment);

    }

    /***
     * 根据id查询
     * @param id
     * @return
     */
    public void  getCommentById(String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        mongoTemplate.findOne(query, Comment.class);
    }

    /**
     * 更新评论
     *
     * @param comment
     * @return
     */
    public void updateComment(Comment comment) {

        Query query = new Query(Criteria.where("_id").is(comment.getId()));
        Update update = new Update().set("content", comment.getContent());


        mongoTemplate.updateFirst(query, update, Comment.class);


    }
    /***
     * 删除
     * @param id
     * @return
     */
    public void deleteComment(String id) {

        Criteria criteria = Criteria.where("_id").is(id);
        Query query = new Query(criteria);
        System.out.println("--------------------------"+query);
        mongoTemplate.remove(query,Comment.class);

    }

    @Override
    public Comment findCommentById(String id) {
        return commentRepository.findById(id).get();
    }

    @Override
    public Page<Comment> findCommentListPageByParentid(String parentid, int page, int size) {
        //为什么要page-1？因为参数穿进去的是zero-based page index.，基于0开始的索引的页码
        return commentRepository.findByParentid(parentid, PageRequest.of(page-1,size));
    }

    @Override
    public void updateCommentLikenumThumbup(String id) {
        //查询对象
        Query query=Query.query(Criteria.where("_id").is(id));
        //更新条件
        Update update = new Update();
        update.inc("likenum");

        //执行更新
        mongoTemplate.updateFirst(query,update,"comment");
    }
}
